#include "button_app.h"
#include "ebtn.h"

const ebtn_btn_param_t default_param_normal = EBTN_PARAMS_INIT(
    20,  // time_debounce: 按下稳定 20ms
    20,  // time_debounce_release: 释放稳定 20ms
    50,  // time_click_pressed_min: 最短单击按下 50ms
    300, // time_click_pressed_max: 最长单击按下 500ms (超过则不算单击)
    200, // time_click_multi_max: 多次单击最大间隔 300ms (两次点击间隔超过则重新计数)
    500, // time_keepalive_period: 长按事件周期 500ms (按下超过 500ms 后，每 500ms 触发一次)
    5    // max_consecutive: 最多支持 5 连击
);

typedef enum
{
    USER_BUTTON_1 = 1,
    USER_BUTTON_2,
    USER_BUTTON_3,
    USER_BUTTON_4,
} button_t;

ebtn_btn_t btns[] = {
    EBTN_BUTTON_INIT(USER_BUTTON_1, &default_param_normal), // KEY1, ID=1, 使用 'default_param_normal' 参数
    EBTN_BUTTON_INIT(USER_BUTTON_2, &default_param_normal), // KEY2, ID=2, 也使用 'default_param_normal' 参数
    EBTN_BUTTON_INIT(USER_BUTTON_3, &default_param_normal), // KEY3, ID=3, 使用 'default_param_normal' 参数
    EBTN_BUTTON_INIT(USER_BUTTON_4, &default_param_normal), // KEY4, 使用 'default_param_normal' 参数
};

uint8_t my_get_button_state(struct ebtn_btn *btn)
{
    // 根据传入的按钮实例中的 key_id 判断是哪个物理按键
    switch (btn->key_id)
    {
    case 1: // 请求读取 KEY0 的状态
        // 假设 KEY1 接在 PB0，按下为低电平 (返回 1 代表按下)
        return !HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
    case 2: // 请求读取 KEY2 的状态
        // 假设 KEY2 接在 PB1，按下为低电平
        return !HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin);
    case 3: // 请求读取 KEY2 的状态
        // 假设 KEY2 接在 PB1，按下为低电平
        return !HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin);
    case 4: // 请求读取 KEY2 的状态
        // 假设 KEY2 接在 PB1，按下为高电平
        return !HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin);
        // ... 可以添加更多按键的读取逻辑 ...
    default:
        // 对于库内部处理组合键等情况，或者未知的 key_id，安全起见返回 0 (未按下)
        return 0;
    }
}
// EBTN_EVT_ONPRESS = 0x00,         /*!< 按下事件 - 检测到有效按下时发送 * /
// EBTN_EVT_ONRELEASE,              /*!< 释放事件一检测到有效释放事件时发送（从活动到非活动）* /
// EBTN_EVT_ONCLICK,                /*!<单击事件一发生有效的按下和释放事件序列时发送*/
// EBTN_EVT_KEEPALIVE,              /*!< 保持活动事件一按钮处于活动状态时定期发送 * /

void my_button_event(struct ebtn_btn *btn, ebtn_evt_t evt)
{
    EventBits_t ev_sleep;
    building_info_t *data = NULL;
    xQueuePeek(building_data_QueueHandle, &data, 0);
    ev_sleep = xEventGroupWaitBits(status_event_group, SYSTEM_SLEEP_EVENT, pdFALSE, pdFALSE, 0);
    // 只处理 单击/双击事件
    if (evt == EBTN_EVT_ONCLICK)
    {
        if (ev_sleep & SYSTEM_SLEEP_EVENT)
        {
            xEventGroupClearBits(status_event_group, SYSTEM_SLEEP_EVENT);
            return;
        }
        OLED_open();
        uint16_t click_cnt = ebtn_click_get_count(btn);
        switch (btn->key_id)
        {
        case USER_BUTTON_1:
            if (click_cnt == 1)
            {
                if (data->select_flag == SELECT_FLOOR_NUM)
                {
                    if ((data->map_num_temp <= SOUTH_4) || (data->map_num_temp >= CENTER_1A && data->map_num_temp <= CENTER_1D))
                        data->floor_num_temp = data->floor_num_temp == 1 ? 7 : --data->floor_num_temp;
                    else if (data->map_num_temp >= SOUTH_5 && data->map_num_temp <= SOUTH_8)
                        data->floor_num_temp = data->floor_num_temp == 1 ? 8 : --data->floor_num_temp;
                    else if (data->map_num_temp >= EAST_20 && data->map_num_temp <= EAST_21)
                        data->floor_num_temp = data->floor_num_temp == 1 ? 12 : --data->floor_num_temp;
                    else if (data->map_num_temp >= EAST_22 && data->map_num_temp <= EAST_23)
                        data->floor_num_temp = data->floor_num_temp == 1 ? 15 : --data->floor_num_temp;
                }
                else if (data->select_flag == SELECT_ROOM_NUM)
                {
                    data->room_num_temp = data->room_num_temp == 1 ? 30 : --data->room_num_temp;
                }
                else if (data->select_flag == SELECT_BUILDING_MAP)
                {
                    data->map_num_temp = data->map_num_temp == 0 ? 15 : --data->map_num_temp;
                }
            }
            break;
        case USER_BUTTON_2:
            if (click_cnt == 1)
            {
                if (data->select_flag == SELECT_FLOOR_NUM)
                {
                    if ((data->map_num_temp <= SOUTH_4) || (data->map_num_temp >= CENTER_1A && data->map_num_temp <= CENTER_1D))
                        data->floor_num_temp = (data->floor_num_temp % 7) + 1;
                    else if (data->map_num_temp >= SOUTH_5 && data->map_num_temp <= SOUTH_8)
                        data->floor_num_temp = (data->floor_num_temp % 8) + 1;
                    else if (data->map_num_temp >= EAST_20 && data->map_num_temp <= EAST_21)
                        data->floor_num_temp = (data->floor_num_temp % 12) + 1;
                    else if (data->map_num_temp >= EAST_22 && data->map_num_temp <= EAST_23)
                        data->floor_num_temp = (data->floor_num_temp % 15) + 1;
                }
                else if (data->select_flag == SELECT_ROOM_NUM)
                {
                    data->room_num_temp = (data->room_num_temp % 30) + 1;
                }
                else if (data->select_flag == SELECT_BUILDING_MAP)
                {
                    data->map_num_temp = ++data->map_num_temp % 16;
                }
            }
            break;
        case USER_BUTTON_3:
            if (click_cnt == 1)
            {
                // 按键3 按下一次进入设置模式 再次按下退出设置模式
                OLED_Clear();
                OLED_Update();
                data->select_flag = data->select_flag == SELECT_BUILDING_MAP ? NO_SELECT : SELECT_BUILDING_MAP;
                if (data->select_flag == SELECT_BUILDING_MAP)
                {
                    xEventGroupSetBits(status_event_group, BUILDING_SET_EVENT); // 设置模式
                }
                else
                {
                    xEventGroupClearBits(status_event_group, BUILDING_SET_EVENT); // 退出设置
                }
            }
            break;
        case USER_BUTTON_4: // 确认键
            if (click_cnt == 1)
            {
                if (data->select_flag != NO_SELECT)
                {
                    if (data->select_flag == SELECT_BUILDING_MAP)
                    {
                        xQueueOverwrite(building_data_QueueHandle, &data);
                    }
                    if (data->select_flag == SELECT_ROOM_NUM) // 设置完房间号 退出 变为NOSELECT
                    {
                        // set_building_map_num(data->map_num_temp);
                        // set_building_floor_num(data->floor_num_temp);
                        // set_building_room_num(data->room_num_temp);
                        xQueueOverwrite(building_data_QueueHandle, &data);
                        OLED_Clear();
                        OLED_Update();
                        xEventGroupClearBits(status_event_group, BUILDING_SET_EVENT); // 确认设置清除设置标志位
                        xTaskNotifyGive(ml307r_get_electricity_handle);
                    }
                    data->select_flag = ++data->select_flag % 4;
                }
            }
            break;
        }
        xQueueOverwrite(building_data_QueueHandle, &data);
    }
    else if (evt == EBTN_EVT_KEEPALIVE) // 长按加加
    {
        switch (btn->key_id)
        {
        case USER_BUTTON_1:
            if (data->select_flag == SELECT_FLOOR_NUM)
            {
                data->floor_num_temp = data->floor_num_temp == 1 ? 15 : --data->floor_num_temp;
            }
            else if (data->select_flag == SELECT_ROOM_NUM)
            {
                data->room_num_temp = data->room_num_temp == 1 ? 30 : --data->room_num_temp;
            }
            else if (data->select_flag == SELECT_BUILDING_MAP)
            {
                data->map_num_temp = data->map_num_temp == 0 ? 15 : --data->map_num_temp;
            }
            break;
        case USER_BUTTON_2:
            if (data->select_flag == SELECT_FLOOR_NUM)
            {
                data->floor_num_temp = (data->floor_num_temp % 15) + 1;
            }
            else if (data->select_flag == SELECT_ROOM_NUM)
            {
                data->room_num_temp = (data->room_num_temp % 30) + 1;
            }
            else if (data->select_flag == SELECT_BUILDING_MAP)
            {
                data->map_num_temp = ++data->map_num_temp % 16;
            }
            break;
        case USER_BUTTON_4:
            xEventGroupSetBits(status_event_group, SYSTEM_SLEEP_EVENT);
            break;
        }
        xQueueOverwrite(building_data_QueueHandle, &data);
    }
}

void button_app_init(void)
{
    ebtn_init(btns, EBTN_ARRAY_SIZE(btns), NULL, 0, my_get_button_state, my_button_event);
}

void btn_task(void *arg)
{
    button_app_init();
    while (1)
    {
        ebtn_process(uwTick);
        vTaskDelay(5);
    }
}
